package base;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * servlet 基类
 */
public class BaseServlet extends HttpServlet {
    protected String TAG = this.getClass().getName();
    protected String param;
    protected JsonObject jsonRequest;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp){
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp){
        try {
            JsonParser parser = new JsonParser();
            jsonRequest = (JsonObject) parser.parse(new InputStreamReader(req.getInputStream()));
            System.out.println(jsonRequest.toString());
            param = jsonRequest.get("param").getAsString();
        } catch (IOException ex) {
            ex.printStackTrace();
            jsonRequest = null;
        }
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
    }

    @Override
    public void destroy() {
    }

    @Override
    public void init() {
        System.out.println(System.currentTimeMillis());
    }

    protected String bean2Json(Object object) {
        return new Gson().toJson(object);
    }
}
